<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('squad.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-form-item :label="i18n('squad.picture')" prop="picture">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handlePictureSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item :label="i18n('squad.title')" prop="title">
                    <el-input :placeholder="i18n('common.please_input') + i18n('squad.title')" v-model="dataForm.title"></el-input>
                  </el-form-item>

                  <el-form-item :label="$t('squad.teacher_name')" prop="teacher_id">
                    <el-select v-model="dataForm.teacher_id" filterable clearable>
                      <el-option v-for="(v,k) in teacherList" :label="v.nickname" :key="k" :value="v.id">
                      </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item :label="$t('squad.start_stop_time')" prop="valid_time">
                    <el-date-picker format="YYYY-MM-DD" v-model="dataForm.valid_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('squad.start_time')" :end-placeholder="$t('squad.end_time')"></el-date-picker>
                  </el-form-item>

                  <el-form-item :label="$t('squad.total')" prop="total">
                    <el-input-number :placeholder="$t('common.please_input')+$t('squad.total')" v-model="dataForm.total" :min="1" :max="300"></el-input-number>
                  </el-form-item>

                  <el-form-item :label="$t('common.sort')" prop="sort">
                    <el-input-number :placeholder="$t('common.please_input')+$t('common.sort')" v-model="dataForm.sort" :min="0"></el-input-number>
                  </el-form-item>

                  <el-form-item :label="i18n('common.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item class="mavon" prop="content" :label="i18n('squad.content')">
                <editor ref="editorRef" :content="dataForm.content"></editor>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:squad:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="squad-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"
  import Editor from '@/components/form/editor'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('squad')

  const editorRef = ref()
  const teacherList = ref([])

  const dataForm = reactive({
    id: 0,
    teacher_id: '',
    picture: '',
    title: '',
    valid_time: '',
    total: '',
    content: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('squad.rules.title.require'),
      trigger: 'blur',
    }],
    valid_time: [{
      required: true,
      message: i18n('squad.rules.valid_time.require'),
      trigger: 'blur',
    }],
    total: [{
      required: true,
      message: i18n('squad.rules.total.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.teacher_id = data.data.teacher_id
            dataForm.picture = data.data.picture
            dataForm.title = data.data.title
            dataForm.valid_time = data.data.valid_time
            dataForm.total = data.data.total
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'squad_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.content = data.data.content
                }
              }
            })
          }
        })
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            teacher_id: dataForm.teacher_id,
            picture: dataForm.picture,
            title: dataForm.title,
            valid_time: dataForm.valid_time,
            total: dataForm.total,
            content: editorRef.value.data,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(i18n(data.message))
          }
        })
      }
    })
  }


  const loadTeacherList = () => {
    $http({
      url: $http.adornUrl('/member/select'),
      method: 'get',
      params: $http.adornParams({
        role_id: 2
      })
    }).then(({ data }) => {
      if (data && data.status === 200) {
        teacherList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadTeacherList()
  })
</script>

<style lang="scss" scoped>
  .admin_form_main .el-form-item{
    .el-input {
      width: 400px;
    }
    .el-select {
      width: 400px;
    }
    .el-input-number {
      width: 400px;
    }
  }
  .form-width-textarea-800 {
    width: 800px;
  }
  .resource {
    width: 95% !important;
  }
</style>

